import Vue from 'vue';
import VueRouter, { RawLocation } from 'vue-router';
import Home from '@/views/appmain/views/home/index.vue';
import AppMain from '@/views/appmain/index.vue';
import PersonalCenter from '@/views/PersonalCenter/index.vue';
import Goods from '@/views/goods/index.vue';
import Login from '@/views/login/index.vue';
import store from '@/store/index.ts';
import request from '@/request/index';
import goTo from 'vuetify/es5/services/goto';
import ShoppingCart from '@/views/ShoppingCart/index.vue';
import FillOrder from '@/views/FillOrder/index.vue';
import Payment from '@/views/Payment/index.vue';
import AddressManagement from '@/views/AddressManagement/index.vue';
import AddressForm from '@/views/AddressForm/index.vue';
import DistPicker from '@/views/DistPicker/index.vue';
import PaymentResult from '@/views/PaymentResult/index.vue';
import MyOrders from '@/views/MyOrders/index.vue';
import GoodsList from '@/views/GoodsList/index.vue';

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location: RawLocation) {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return (originalPush.call(this, location) as any).catch((err: any) => err);
};

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    component: AppMain,
    meta: {
      requireAuth: true
    },
    children: [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'Home',
        component: Home
      },
      {
        path: '/category',
        component: () => import('../views/appmain/views/category/index.vue')
      },
      {
        path: '/profile',
        name: 'Profile',
        component: () => import('../views/appmain/views/profile/index.vue')
      }
    ]
  },
  {
    path: '/personalCenter',
    name: 'PersonalCenter',
    component: PersonalCenter,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/goods',
    name: 'Goods',
    component: Goods,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/shoppingCart',
    name: 'ShoppingCart',
    component: ShoppingCart,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/fillOrder',
    name: 'FillOrder',
    component: FillOrder,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/payment',
    name: 'Payment',
    component: Payment,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/addresses',
    name: 'AddressManagement',
    component: AddressManagement,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/addresses/form',
    name: 'AddressForm',
    component: AddressForm,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/distPicker',
    name: 'DistPicker',
    component: DistPicker,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/paymentResult',
    name: 'PaymentResult',
    component: PaymentResult,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/myOrders',
    name: 'MyOrders',
    component: MyOrders,
    meta: {
      requireAuth: true
    }
  },
  {
    path: '/goodsList',
    name: 'GoodsList',
    component: GoodsList,
    meta: {
      requireAuth: true
    }
  }
];

const router = new VueRouter({
  scrollBehavior: (to, from, savedPosition) => {
    let scrollTo = 0;

    if (to.hash) {
      scrollTo = Number(to.hash);
    } else if (savedPosition) {
      scrollTo = savedPosition.y;
    }

    return goTo(scrollTo);
  },
  routes
});

router.beforeEach((to, from, next) => {
  const isLogin = store.state.currentUser !== null;

  if (to.path == '/login') {
    if (isLogin) {
      next('/');
    } else {
      next();
    }
  } else {
    // requireAuth:可以在路由元信息指定哪些页面需要登录权限
    if (to.meta.requireAuth && isLogin) {
      next();
    } else {
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      request.get('/auth').then((res: any) => {
        if (res && res.id) {
          store.commit('setCurrentUser', res);
          next();
        } else {
          store.commit('clearCurrentUser');
          next('/login');
        }
      });
    }
  }
});

export default router;
